﻿<!doctype html>
<html lang="en">

	<head>
		
		<title>聊天界面</title>
		<link rel="stylesheet" type="text/css" href="/css/chat.css" />
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/flexible.js"></script>
		<script src="/js/common.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script>
            $(function () {
                //先接收共享过来的用户数据
                var userStr = sessionStorage.getItem("user")
                var user = JSON.parse(userStr);
                var params=getParams();
                $.get("/users/"+params.informId+"/user",function (data) {
                    console.log(data.nickName);
                    $(".tit").html(data.nickName)
                })

                $.get("/messages/getPriMsg/"+user.id+"/"+params.informId,function (data) {
                    console.log(data);
					var html="";
					$.each(data,function (index, ele) {
                        if (ele.inform.id == user.id) {

                           html='<div class="show">'+
								   '<div class="msg">'+
								   '<img src="'+ele.inform.headImgUrl+'"/>'+
								   '<p><i class="msg_input"></i>'+ele.content+'</p>'+
								   '</div></div>'
							$(".message").append(html)

                        }else {

                            html='<div class="send">'+
                                '<div class="msg">'+
                                '<img src="'+ele.inform.headImgUrl+'"/>'+
                                '<p><i class="msg_input"></i>'+ele.content+'</p>'+
                                '</div></div>'
                            $(".message").append(html)

						}
                    })
                    //标记已读
                    $.ajax({
                        url: "/messages/setHasRead",
                        data:{userId: user.id, informId: params.informId},
                        type: "post",
                        success: function () {
                            console.log(true);
                        }
                    })
                })
                //自动刷新
               /* setTimeout(function () {
                    location.reload()
                },2500)*/
				function save() {
                    var content=$("#myContent").val()
                    if (!content.replace(/^\s*/,"").length==0) {
                        $.post("/messages/insertPriMsg", {informId: user.id, userId: params.informId, content: content},
                            function (data) {
                                if (data.success) {
                                    location.reload()
                                }

                            })
                    }else {
                        $(document).dialog({
                            type : 'confirm',
                            style: 'default',  // default、ios、android
                            titleText: '错误!',
                            content: "输入为空!请重新输入",
                        });
                    }

                }
                $("#sendBtn").click(function () {
                    save();
                    $("#myContent").val("")
                })


                document.onkeydown = function (event) {
                    var e = event || window.event;
                    if (e && e.keyCode == 13) { //回车键的键值为13
                      save();
                        $("#myContent").val("")
                    }
                };

            })

		</script>
	</head>

	<body>
		<header class="header">
			<h5 class="tit"></h5>
			<a class="back" href="/mine/privateLetter.html"></a>
		</header>
		<div class="message">


		</div>
		<div class="footer">
			<input id="myContent" type="text" style="font-size: 30px"/>
			<span id="sendBtn"><p>发送</p></span>
		</div>
	</body>


</html>